//
//  YMCreditLeaseVC.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/14.
//  Copyright © 2018年 youmy. All rights reserved.
//  信用租赁

import UIKit

import MJRefresh

fileprivate let kCellId = "kCellId"

fileprivate let kFooterId = "kFooterId"

fileprivate let width = UIScreen.main.bounds.width

fileprivate let height = UIScreen.main.bounds.height

fileprivate let kItemMargin:CGFloat = 1

fileprivate let kItemW:CGFloat = (UIScreen.main.bounds.width - 1 * kItemMargin) / 2

class YMCreditLeaseVC: UIViewController {
    
    lazy var headerView = YMCreditLeaseHeaderView.nibView()
    
    lazy var noDataView = YMNoMoreDataView.nibView()
    
    lazy var brandMenuV:YMMenuCollectionView = {
        let brandV = YMMenuCollectionView(frame: CGRect(x: 0, y: 0, width: ym_screen_width, height: 20))
        return brandV
    }()
    
    lazy var colorMenuV:YMMenuCollectionView = {
        let brandV = YMMenuCollectionView(frame: CGRect(x: 0, y: 0, width: ym_screen_width, height: 20))
        return brandV
    }()
    
    var brandBtn:UIButton?
    
    var colourBtn:UIButton?
    
    lazy var maskView:UIView = {
        let maskView = UIView(frame: self.view.bounds)
        maskView.backgroundColor = UIColor.init(white: 0.3, alpha: 0.7)
        
        let height:CGFloat = 90 + 40 + 2 * 13
        let frame = CGRect(x: 0, y: 104, width: UIScreen.main.bounds.width, height: height)
        
        brandMenuV.frame = frame
        colorMenuV.frame = frame
        
        return maskView
    }()
    
    lazy var collectionView: UICollectionView = {
        
        let H = height - headerView.bounds.height
        
        let layout = UICollectionViewFlowLayout()
        
        layout.scrollDirection = .vertical
        
        layout.minimumLineSpacing = kItemMargin
        
        layout.minimumInteritemSpacing = kItemMargin
        
        layout.sectionInset = UIEdgeInsetsMake(kItemMargin, 0, kItemMargin, 0)
        
        layout.itemSize = CGSize(width: kItemW, height: 200)
        
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: headerView.frame.maxY, width: width, height: H), collectionViewLayout: layout)
        
        collectionView.backgroundColor = ym_bg_color
        
        collectionView.register(UINib.init(nibName: "YMHomeNormalCell", bundle: nil), forCellWithReuseIdentifier: kCellId)
        
        collectionView.dataSource = self
        
        collectionView.delegate = self
        
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        
        return collectionView
    }()

    override func viewDidLoad() {
        
        super.viewDidLoad()

        self.view.addSubview(collectionView)
        
        self.view.addSubview(headerView)
        
        brandMenuV.menus = ["苹果","OPPO","VIVO","华为","三星","坚果","一加","小米","魅族","美图"]
        
        colorMenuV.menus = ["全新","99新","95新","9新"]
        
        refresh()
        
        menuProccess()
    }
    
}


// MARK: - 下拉、上拉刷新
extension YMCreditLeaseVC {
    func refresh(){
        
        collectionView.mj_header = YMRefresh.headerRefresh(self, #selector(self.lonaNew))
        
        collectionView.mj_footer = YMRefresh.footerRefresh(self, #selector(self.loadMore))
    }
    
    @objc func lonaNew(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.collectionView.mj_header.endRefreshing()
        })
    }
    
    @objc func loadMore(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.collectionView.mj_footer.endRefreshingWithNoMoreData()
        })
    }
}


// MARK: - 下拉菜单
extension YMCreditLeaseVC{
    
    func menuProccess(){
        headerView.brandBtnBlock = {[weak self] (btn) in
            
            if self?.colourBtn?.isSelected == true{
                self?.colourBtn?.isSelected = false
            }
            self?.colorMenuV.isHidden = true
            self?.brandMenuV.isHidden = false
            self?.brandMenuV.bringSubview(toFront: (self?.colorMenuV)!)
            if btn.isSelected == true {
                self?.brandBtn = btn
                self?.brandBtn?.setTitleColor(ym_red_color, for: .normal)
                self?.maskView.addSubview((self?.brandMenuV)!)
                self?.insert()
            }else{
                self?.removeMaskView()
            }
        }
        
        headerView.colourBtnBlock = {[weak self] (btn) in
            if self?.brandBtn?.isSelected == true{
                self?.brandBtn?.isSelected = false
            }
            self?.brandMenuV.isHidden = true
            self?.colorMenuV.isHidden = false
            self?.colorMenuV.bringSubview(toFront: (self?.brandMenuV)!)
            if btn.isSelected == true {
                self?.colourBtn = btn
                self?.colourBtn?.setTitleColor(ym_red_color, for: .normal)
                self?.maskView.addSubview((self?.colorMenuV)!)
                self?.insert()
            }else{
                self?.removeMaskView()
            }
            
        }
        
        brandMenuV.didSelBlcok = {[weak self] (title) in
            self?.brandBtn?.setTitle(title, for: .normal)
            self?.brandBtn?.isSelected = !(self?.brandBtn?.isSelected)!
            self?.removeMaskView()
        }
        
        colorMenuV.didSelBlcok = {[weak self] (title) in
            self?.colourBtn?.setTitle(title, for: .normal)
            self?.colourBtn?.isSelected = !(self?.colourBtn?.isSelected)!
            self?.removeMaskView()
        }
    }
    
    
    func insert(){
        view.insertSubview(maskView, belowSubview: headerView)
    }
    
    func removeMaskView(){
        brandMenuV.removeFromSuperview()
        colorMenuV.removeFromSuperview()
        maskView.removeFromSuperview()
    }
}

extension YMCreditLeaseVC: UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellId, for: indexPath)
        return cell
    }
}
